#include "XMLElement.h"
#include "../commun.h"

#include <iostream>
using namespace std;


XMLElement::XMLElement(std::string nodeName)
{
	this->nodeName = nodeName;
}

XMLElement::~XMLElement()
{
	cout << "DESTRUCTTTTTTTTTTTTION GODZILLA TSUNAMI" << endl;
}

void XMLElement::addAttribute(std::string attName, std::string attValue)
{
	this->attributes.push_back(make_pair(attName, attValue));
}

void XMLElement::setAttributes(AttList &attr)
{
	attributes = attr;
}

void XMLElement::addChild(XMLNode* child)
{
	this->childList.push_back(child);
	child->setParent(this);
}

vector<XMLNode*> XMLElement::getChildList()
{
	return childList;
}

string XMLElement::toString()
{
	string R = "";

	//Début du tag
	R = "<" + this->nodeName;

	//Ajout des attributs
	R += attributesToString();

	//Fin du tag
	R += ">\n";

	//Enfants
	for (unsigned int i = 0; i < this->childList.size(); i++)
	{
		R += this->childList.at(i)->toString();
		R += "\n";
	}

	//Fin de balise
	R += "</" + this->nodeName + ">";

	return R;
}

string XMLElement::attributesToString()
{
	string R = "";

	//liste des attributs
	AttList::iterator it;
	for (it=attributes.begin(); it!=attributes.end(); it++)
	{
		R += " " + it->first;
		if (it->second != "")
		{
			R += "=\"" + it->second + "\"";
		}
	}
	
	return R;
}

string XMLElement::getNodeName()
{
	return this->nodeName;
}

AttList XMLElement::getAttributes()
{
	return this->attributes;
}
